package local.sumw3.findmydevices;

import local.sumw3.findmydeivces.R;
import local.sumw3.findmydevices.object.FindMyDevices;
import android.app.DialogFragment;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class ConsoleFragment extends DialogFragment {

    static ConsoleFragment newInstance() {
        ConsoleFragment f = new ConsoleFragment();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_console, container, false);
        TextView consoleTxt = (TextView) v.findViewById(R.id.consoleTxt);
        FindMyDevices findDevice = ((MainActivity) getActivity()).getFindDevice();
        if (findDevice != null) {
            consoleTxt.setText(findDevice.getResponseText());
        } else {
            consoleTxt.setText(getString(R.string.msg_noDeviceFound));
        }
        consoleTxt.setMovementMethod(ScrollingMovementMethod.getInstance());
        v.findViewById(R.id.consoleOKBtn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ConsoleFragment f = (ConsoleFragment) getFragmentManager().findFragmentByTag(
                        UIConstants.FRAGMENT_TAG_MAINACTIVITY);
                if (f != null) {
                    f.dismiss();
                }
            }
        });
        return v;
    }
}
